package com.oocl.chat.dao.impl;

import java.io.BufferedWriter;
import java.io.IOException;
import java.sql.Connection;
import java.util.Map;

import com.oocl.chat.dao.WorkOperate;
import com.oocl.chat.db.DBUtil;
import com.oocl.chat.pojo.Message;
import com.oocl.chat.util.JsonConvertor;
import com.oocl.chat.util.MaintainMap;
import com.oocl.chat.util.MapOperate;

public class WorkOperateImpl implements WorkOperate {
	
	public void downline(Message message,BufferedWriter writers) {
		MapOperate.removeUser(message.getSender());
		sendAllExcepItself(message,writers);
		System.out.println("用户"+ message.getSender()+"下线！！！");
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void online(Message message,BufferedWriter writers) {
		String[] info = message.getContent().split(",");
		UserOperateImpl uoi = new UserOperateImpl();
		int successFlag = uoi.findUserByPassName(info[0], info[1]);
		//失败
		if(successFlag == 0){
			message.setContent("0");
			sendBack(message,writers);
			return;
		}
//		//成功
//		else if(successFlag ==1){
//			message.setContent("1");
//		}
		MapOperate.saveUser(message.getSender(), writers);
		String userList = MapOperate.getUserList(message.getSender());
		sendAllExcepItself(message,writers);
		message.setType(4);
		message.setContent(userList);
		message.setReceiver(message.getSender());
		sendSpecify(message,writers);
		System.out.println(message.getSender()+"登录成功！");
	}

	public void shake(Message message,BufferedWriter writers) {
		sendAllExcepItself(message,writers);
		System.out.println(message.getSender()+" 发出震屏!!!");
	}

	public void chat(Message message,BufferedWriter writers) {
		sendMessage(message,writers);
	}
	private void sendMessage(Message message,BufferedWriter writers){
		if((!"*".equals(message.getReceiver()))&&message.getType()==0)
		{
			sendSpecify(message,writers);
		}else{
			sendAll(message,writers);
		}
	}
	//发送所有人
		private void sendAll(Message message,BufferedWriter writers) {
			Map<String, BufferedWriter> mapList = MaintainMap.getInstance().getMap();
			for(String s: mapList.keySet()){
				writers = mapList.get(s);
				try {
					
					writers.write(new JsonConvertor(message).getJsonString()+"\n");
					writers.flush();
				} catch (IOException e) {
//					e.printStackTrace();
					try {
						writers.close();
					} catch (IOException e1) {
//						e1.printStackTrace();
						System.out.println("关闭流");
					}
				}
				System.out.println("用户"+message.getSender()+"发送信息到所有client成功");
				System.out.println(mapList.get(s));
			}
		}
		//发送指定的人
		private void sendSpecify(Message message,BufferedWriter writers){
			Map<String, BufferedWriter> mapList = MaintainMap.getInstance().getMap();
			for(String s: mapList.keySet()){
				if(s.equals(message.getReceiver())){
					writers = mapList.get(s);
					message.setReceiver("我");
					try {
						writers.write(new JsonConvertor(message).getJsonString()+"\n");
						writers.flush();
					} catch (Exception e) {
						try {
							writers.close();
						} catch (IOException e1) {
//							e1.printStackTrace();
							System.out.println("关闭流");
						}
					}
					System.out.println(message.getSender()+" 发送信息给 "+message.getReceiver()+"成功");
				}
			}
		}
		//发送给除了自己的所有人
		private void sendAllExcepItself(Message message,BufferedWriter writers){
			Map<String, BufferedWriter> mapList = MaintainMap.getInstance().getMap();
			for(String s:mapList.keySet()){
				if(!s.equals(message.getSender())){
					writers = mapList.get(s);
					try {
						writers.write(new JsonConvertor(message).getJsonString()+"\n");
						writers.flush();
					} catch (Exception e) {
						try {
							writers.close();
						} catch (IOException e1) {
//							e1.printStackTrace();
							System.out.println("关闭流");
						}
					}
				}
			}
		}

		public void register(Message message, BufferedWriter writer) {
			UserOperateImpl uoi = new UserOperateImpl();
			int successFlag = uoi.addUser(message);
			//失败
			if(successFlag == 0){
				message.setContent("0");
				sendBack(message,writer);
			}
			//成功
			else if(successFlag ==1){
				message.setContent("1");
				sendBack(message,writer);
			}
		}
		//登录注册时 返回给自己
		private void sendBack(Message message,BufferedWriter writers){
			try {
				writers.write(new JsonConvertor(message).getJsonString()+"\n");
				writers.flush();
			} catch (IOException e) {
				try {
					writers.close();
				} catch (IOException e1) {
//					e1.printStackTrace();
					System.out.println("关闭流");
				}
			}
		}

}
